package com.jack006.stock.controller;

import com.jack006.api.controller.stock.StockControllerApi;
import com.jack006.grace.result.GraceJSONResult;
import com.jack006.grace.result.ResponseStatusEnum;
import com.jack006.stock.service.StockNewsService;
import com.jack006.stock.service.StockService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Classname HelloController
 * @Description 接口实现类
 * @Date 2021-06-10 08:15
 * @Created by jack
 */
@RestController
public class StockController implements StockControllerApi {

    final static Logger logger = LoggerFactory.getLogger(StockController.class);


    @Autowired
    private StockService stockService;
    @Autowired
    private StockNewsService stockNewsService;

    @Override
    public GraceJSONResult recordFlushInfo(String code) {
        stockService.recordFlushInfo(code);
        return GraceJSONResult.ok("保存实时数据成功");
    }

    @Override
    public GraceJSONResult recordDayInfo(String code) {
        if(StringUtils.isBlank(code)) {
            return GraceJSONResult.errorCustom(ResponseStatusEnum.STOCK_CODE_IS_NULL_ERROR);
        }
        stockService.recordStockDayInfo(code);
        return GraceJSONResult.ok("保存每日数据成功");
    }

    @Override
    public GraceJSONResult createStock(String code) {
        if(StringUtils.isBlank(code)) {
            return GraceJSONResult.errorCustom(ResponseStatusEnum.STOCK_CODE_IS_NULL_ERROR);
        }
        stockService.createStock(code);
        return GraceJSONResult.ok("保存股票信息成功");
    }

    @Override
    public GraceJSONResult searchStockNews(String time) {
        Long timeStamp;
        if (StringUtils.isBlank(time)) {
            timeStamp = System.currentTimeMillis()/1000;
        } else {
            timeStamp = Long.parseLong(time);
        }
        stockNewsService.searchStockNews(timeStamp,1,20);
        return GraceJSONResult.ok("获取股票快讯成功");
    }
}
